package com.dhcc.cdc.busi.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcDictionaryMapper;
import com.dhcc.cdc.busi.dao.CdcDictypeMapper;
import com.dhcc.cdc.busi.dao.CdcEpisodeMapper;
import com.dhcc.cdc.busi.dao.CdcHospitalMapper;
import com.dhcc.cdc.busi.dao.CdcLocationMapper;
import com.dhcc.cdc.busi.dao.CdcUserMapper;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.entity.CdcDictypeEntity;
import com.dhcc.cdc.busi.entity.CdcEpisodeEntity;
import com.dhcc.cdc.busi.entity.CdcHospitalEntity;
import com.dhcc.cdc.busi.entity.CdcLocationEntity;
import com.dhcc.cdc.busi.entity.CdcUserEntity;
import com.dhcc.cdc.busi.service.ICdcEpisodeService;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.cdc.util.CommonSrv;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;

/**
 * 患者就诊信息表数据服务层接口
 * 
 * @ClassName: CdcEpisodeServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:08
 */
@Service("cdcEpisodeService")
@SuppressWarnings("unchecked")
public class CdcEpisodeServiceImpl extends BaseServiceImpl<CdcEpisodeMapper, CdcEpisodeEntity>
        implements ICdcEpisodeService {

	@Autowired
    private CdcDictionaryMapper cdcDictionaryMapper;
    
    @Autowired
    private CdcDictypeMapper cdcDictypeMapper;
    
    @Autowired
    private CdcHospitalMapper cdcHospitalMapper;
    
    @Autowired
    private CdcLocationMapper cdcLocationMapper;
    
    @Autowired
    private CdcUserMapper cdcUserMapper;
    
    @Autowired
    private CdcDataMapper cdcDataMapper;
	@Autowired
    private ICdcDictionaryService cdcDictionaryService;

    @Override
    public List<CdcEpisodeEntity> page(Page<CdcEpisodeEntity> page, Map<String, Object> map) {
        List<CdcEpisodeEntity> result = this.baseMapper.page(page, map);
        String organizationId = (String) map.get("organizationId");
        if ((organizationId != null) && (organizationId != "") && (!"0".equals(organizationId))) {
            result = (List<CdcEpisodeEntity>) CommonSrv.filtByOrgId(result, organizationId);
        }

        return result;
    }

    @Override
    public List<CdcEpisodeEntity> list(Map<String, Object> map) {
        List<CdcEpisodeEntity> result = this.baseMapper.list(map);
        String organizationId = (String) map.get("organizationId");
        if ((organizationId != null) && (organizationId != "") && (!"0".equals(organizationId))) {
            result = (List<CdcEpisodeEntity>) CommonSrv.filtByOrgId(result, organizationId);
        }

        return result;
    }

    @Override
    public List<CdcEpisodeEntity> listByDate(String dateType, String startDate, String endDate, Map<String, Object> map) {
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("dateType", dateType);
		CdcDictionaryEntity busAdmStatus = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_status", "C");
		CdcDictionaryEntity busAdmType = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_type", "I");
		map.put("busAdmStatus", busAdmStatus.getId());
		map.put("busAdmType", busAdmType.getId());
		List<CdcEpisodeEntity> cdcEpisodeEntities = this.baseMapper.listByDate(map);
        /*List<CdcEpisodeEntity> result = new ArrayList<>();
        for (CdcEpisodeEntity cdcEpisodeEntity : cdcEpisodeEntities){
            Long busAdmTypeId = cdcEpisodeEntity.getBusAdmTypeId();
            Long busAdmStatusId = cdcEpisodeEntity.getBusAdmStatusId();
            CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(busAdmTypeId);
            CdcDictionaryEntity cdcDictionaryEntity1 = cdcDictionaryMapper.selectById(busAdmStatusId);
            if ("I".equals(cdcDictionaryEntity.getCode()) && !"C".equals(cdcDictionaryEntity1.getCode())){
                result.add(cdcEpisodeEntity);
            }
        }*/

        return cdcEpisodeEntities;
    }

    @Override
    public List<CdcEpisodeEntity> getFPCodingAdmsByDate(Map<String,Object> map) {
        map.put("isCoded", 1);
        List<CdcEpisodeEntity> cdcEpisodeEntities = this.baseMapper.selectByCodedAndDate(map);
        List<CdcEpisodeEntity> result = new ArrayList<>();
        for (CdcEpisodeEntity cdcEpisodeEntity : cdcEpisodeEntities){
            Long busAdmTypeId = cdcEpisodeEntity.getBusAdmTypeId();
            Long busAdmStatusId = cdcEpisodeEntity.getBusAdmStatusId();
            CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(busAdmTypeId);
            CdcDictionaryEntity cdcDictionaryEntity1 = cdcDictionaryMapper.selectById(busAdmStatusId);
            if (Objects.equals("I", cdcDictionaryEntity.getCode()) && !Objects.equals("C", cdcDictionaryEntity1.getCode())){
                result.add(cdcEpisodeEntity);
            }
        }
        return result;
    }
    /**
     * 
     * <p>Title: handleXmlData</p>   
     * <p>Description: </p>   
     * @param dataStr
     * @return   
     * @see com.dhcc.cdc.busi.service.ICdcEpisodeService#handleXmlData(java.lang.String)   
     *
     */
	@Override
	public String handleXmlData(String dataStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Document doc = null;
        try {
            doc = DocumentHelper.parseText(dataStr); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            String xcode = rootElt.elementTextTrim("xcode"); //索引码
            String episodenum = rootElt.elementTextTrim("episodenum"); //就诊流水号
            String patientcode = rootElt.elementTextTrim("patientcode"); //患者主索引
            String busadmtypecode = rootElt.elementTextTrim("busadmtypecode"); //就诊类型
            String busadmtypedesc = rootElt.elementTextTrim("busadmtypedesc"); //就诊类型
            String busadmstatuscode = rootElt.elementTextTrim("busadmstatuscode"); //就诊状态
            String busadmstatusdesc = rootElt.elementTextTrim("busadmstatusdesc"); //就诊状态
            String healthcode = rootElt.elementTextTrim("healthcode"); //电子健康码
            String name = rootElt.elementTextTrim("name"); //姓名
            String gender = rootElt.elementTextTrim("gender"); //性别
            String nationality = rootElt.elementTextTrim("nationality"); //国籍
            String nation = rootElt.elementTextTrim("nation"); //民族
            String birthdatestr = rootElt.elementTextTrim("birthdate"); //出生日期
            String age = rootElt.elementTextTrim("age"); //年龄
            String cardtype = rootElt.elementTextTrim("cardtype"); //证件类型
            String cardvalue = rootElt.elementTextTrim("cardvalue"); //证件号码
            String nativeplace = rootElt.elementTextTrim("nativeplace"); //籍贯
            String company = rootElt.elementTextTrim("company"); //工作单位
            String telephone = rootElt.elementTextTrim("telephone"); //电话
            String email = rootElt.elementTextTrim("email"); //邮箱
            String contactor = rootElt.elementTextTrim("contactor"); //联系人
            String contactorphone = rootElt.elementTextTrim("contactorphone"); //联系人电话
            String career = rootElt.elementTextTrim("career"); //职业
            String bloodabo = rootElt.elementTextTrim("bloodabo"); //血型ABO
            String bloodrh = rootElt.elementTextTrim("bloodrh"); //血型Rh
            String registnum = rootElt.elementTextTrim("registnum"); //登记号
            String mrnum = rootElt.elementTextTrim("mrnum"); //病案号
            String nativeprovince = rootElt.elementTextTrim("nativeprovince"); //户籍所在地省
            String nativecity = rootElt.elementTextTrim("nativecity"); //户籍所在地市
            String nativecounty = rootElt.elementTextTrim("nativecounty"); //户籍所在地县
            String nativetown = rootElt.elementTextTrim("nativetown"); //户籍所在地乡
            String nativeaddress = rootElt.elementTextTrim("nativeaddress"); //户籍所在地详细描述
            String nowprovince = rootElt.elementTextTrim("nowprovince"); //现住址所在地省
            String nowcity = rootElt.elementTextTrim("nowcity"); //现住址所在地市
            String nowcounty = rootElt.elementTextTrim("nowcounty"); //现住址所在地县
            String nowtown = rootElt.elementTextTrim("nowtown"); //现住址所在地乡
            String nowaddress = rootElt.elementTextTrim("nowaddress"); //现住址详细描述
            String isdeathstr = rootElt.elementTextTrim("isdeath"); //死亡标志
            String deathdatestr = rootElt.elementTextTrim("deathdate"); //死亡日期
            String admnumsstr = rootElt.elementTextTrim("admnums"); //住院次数
            String admdatestr = rootElt.elementTextTrim("admdate"); //入院日期
            String admhospid = rootElt.elementTextTrim("admhospid"); //就诊院区
            String admlocid = rootElt.elementTextTrim("admlocid"); //就诊科室
            String admlocdesc = rootElt.elementTextTrim("admlocdesc"); //就诊科室Desc
            String admwardid = rootElt.elementTextTrim("admwardid"); //就诊病区
            String admwarddesc = rootElt.elementTextTrim("admwarddesc"); //就诊病区Desc
            String admroom = rootElt.elementTextTrim("admroom"); //就诊房间
            String admbed = rootElt.elementTextTrim("admbed"); //就诊床位
            String admdoctorid = rootElt.elementTextTrim("admdoctorid"); //主管医生
            String admdoctorname = rootElt.elementTextTrim("admdoctorname"); //主管医生姓名
            String dishdatestr = rootElt.elementTextTrim("dishdate"); //出院日期
            String admdaysstr = rootElt.elementTextTrim("admdays"); //住院天数
            String isnewlystr = rootElt.elementTextTrim("isnewly"); //是否初诊
            String isnewbornstr = rootElt.elementTextTrim("isnewborn"); //是否新生儿
            String bornweightstr = rootElt.elementTextTrim("bornweight"); //新生儿出生体重(g)
            String admweightstr = rootElt.elementTextTrim("admweight"); //新生儿入院体重(g)
            String iscodedstr = rootElt.elementTextTrim("iscoded"); //是否已编目
            String codedatestr = rootElt.elementTextTrim("codedate"); //编目日期
            String updatetimestr= rootElt.elementTextTrim("updatetime"); //更新时间
            String updateuserid= rootElt.elementTextTrim("updateuserid"); //更新人
            String opercode= rootElt.elementTextTrim("opercode"); //操作码
            
            CdcEpisodeEntity episode = new CdcEpisodeEntity();
            episode.setXcode(xcode);
            //获取dataid
        	CdcDataEntity cdcDataEntity = new CdcDataEntity();
        	cdcDataEntity.setCode("PatData");
        	cdcDataEntity.setIsActive(1);
        	cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
    		Long dataId = cdcDataEntity.getId();
        	//获取basedataid
			CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
			cdcDataEntityBase.setCode("HisBase");
			cdcDataEntityBase.setIsActive(1);
			cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
    		Long basedataId = cdcDataEntityBase.getId();
    		if(CommonUtil.isNotEmpty(updatetimestr)) {
        		Date updatetime = sdf.parse(updatetimestr);
        		episode.setUpdateDate(updatetime);
        	}
        	//获取更新人
        	if(CommonUtil.isNotEmpty(updateuserid)) {
        		CdcUserEntity cdcUserEntity = new CdcUserEntity();
        		cdcUserEntity.setXcode(updateuserid);
        		cdcUserEntity.setDataId(basedataId);
        		cdcUserEntity.setIsActive(1);
        		cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
        		//判断人员存在不存在
        		Long updateuser = null;
        		if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
        			cdcUserEntity = new CdcUserEntity();
        			cdcUserEntity.setXcode(updateuserid);
            		cdcUserEntity.setIsActive(1);
        			cdcUserEntity.setCode("newuser"+updateuserid);
        			cdcUserEntity.setName("newuser"+updateuserid);
            		cdcUserEntity.setDataId(basedataId);
            		//保存用户
            		cdcUserMapper.insert(cdcUserEntity);
            		updateuser = cdcUserEntity.getId();
        		}else {
        			updateuser = cdcUserEntity.getId();
        		}
        		episode.setUpdateBy(updateuser);
        	}
            if("1".equals(opercode)) {//保存/修改
            	if(CommonUtil.isNotEmpty(episodenum)) {
            		episode.setEpisodeNum(episodenum);
            	}
            	if(CommonUtil.isNotEmpty(patientcode)) {
            		episode.setPatientCode(patientcode);
            	}
            	//获取ID
            	if(CommonUtil.isNotEmpty(busadmtypecode)) {
            		CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                	cdcDictypeEntity.setCode("bus_adm_type");
                	cdcDictypeEntity.setIsActive(1);
                	cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
            		Long dicadmtypeId = cdcDictypeEntity.getId();
            		//如果没有对应ID就要插入
            		CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
            		cdcDictionaryEntity.setDictypeId(dicadmtypeId);
            		cdcDictionaryEntity.setCode(busadmtypecode);
            		cdcDictionaryEntity.setIsActive(1);
            		cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
            		//判断字典存在不存在
            		Long busadmtypeid = null;
            		if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
            			cdcDictionaryEntity = new CdcDictionaryEntity();
            			cdcDictionaryEntity.setDictypeId(dicadmtypeId);
            			cdcDictionaryEntity.setCode(busadmtypecode);
            			cdcDictionaryEntity.setDescription(busadmtypedesc);
            			cdcDictionaryEntity.setIsActive(1);
                		//保存字典
            			cdcDictionaryMapper.insert(cdcDictionaryEntity);
            			busadmtypeid = cdcDictionaryEntity.getId();
            		}else {
            			busadmtypeid = cdcDictionaryEntity.getId();
            		}
                	episode.setBusAdmTypeId(busadmtypeid);
            	}
            	//获取ID
            	if(CommonUtil.isNotEmpty(busadmstatuscode)) {
            		CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
            		cdcDictypeEntity.setCode("bus_adm_status");
                	cdcDictypeEntity.setIsActive(1);
                	cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
            		Long dicadmstatusId = cdcDictypeEntity.getId();
            		//如果没有对应ID就要插入
            		CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
            		cdcDictionaryEntity.setDictypeId(dicadmstatusId);
            		cdcDictionaryEntity.setCode(busadmstatuscode);
            		cdcDictionaryEntity.setIsActive(1);
            		cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
            		//判断字典存在不存在
            		Long busadmstatusid = null;
            		if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
            			cdcDictionaryEntity = new CdcDictionaryEntity();
            			cdcDictionaryEntity.setDictypeId(dicadmstatusId);
            			cdcDictionaryEntity.setCode(busadmstatuscode);
            			cdcDictionaryEntity.setDescription(busadmstatusdesc);
            			cdcDictionaryEntity.setIsActive(1);
                		//保存字典
            			cdcDictionaryMapper.insert(cdcDictionaryEntity);
            			busadmstatusid = cdcDictionaryEntity.getId();
            		}else {
            			busadmstatusid = cdcDictionaryEntity.getId();
            		}
                	episode.setBusAdmStatusId(busadmstatusid);
            	}
            	if(CommonUtil.isNotEmpty(healthcode)) {
            		episode.setHealthCode(healthcode);
            	}
            	
            	if(CommonUtil.isNotEmpty(name)) {
            		episode.setName(name);
            	}
            	if(CommonUtil.isNotEmpty(gender)) {
            		episode.setGender(gender);
            	}
            	if(CommonUtil.isNotEmpty(nationality)) {
            		episode.setNationality(nationality);
            	}
            	if(CommonUtil.isNotEmpty(nation)) {
            		episode.setNation(nation);
            	}
            	if(CommonUtil.isNotEmpty(birthdatestr)) {
            		Date birthdate = sdf.parse(birthdatestr);
            		episode.setBirthdate(birthdate);
            	}
            	if(CommonUtil.isNotEmpty(age)) {
            		episode.setAge(age);
            	}
            	if(CommonUtil.isNotEmpty(cardtype)) {
            		episode.setCardType(cardtype);
            	}
            	if(CommonUtil.isNotEmpty(cardvalue)) {
            		episode.setCardValue(cardvalue);
            	}
            	if(CommonUtil.isNotEmpty(nativeplace)) {
            		episode.setNativePlace(nativeplace);
            	}
            	if(CommonUtil.isNotEmpty(company)) {
            		episode.setCompany(company);
            	}
            	if(CommonUtil.isNotEmpty(telephone)) {
            		episode.setTelephone(telephone);
            	}
            	if(CommonUtil.isNotEmpty(email)) {
            		episode.setEmail(email);
            	}
            	if(CommonUtil.isNotEmpty(contactor)) {
            		episode.setContactor(contactor);
            	}
            	if(CommonUtil.isNotEmpty(contactorphone)) {
            		episode.setContactorPhone(contactorphone);
            	}
            	if(CommonUtil.isNotEmpty(career)) {
            		episode.setCareer(career);
            	}
            	if(CommonUtil.isNotEmpty(bloodabo)) {
            		episode.setBloodAbo(bloodabo);
            	}
            	if(CommonUtil.isNotEmpty(bloodrh)) {
            		episode.setBloodRh(bloodrh);
            	}
            	if(CommonUtil.isNotEmpty(registnum)) {
            		episode.setRegistNum(registnum);
            	}
            	if(CommonUtil.isNotEmpty(mrnum)) {
            		episode.setMrNum(mrnum);
            	}
            	if(CommonUtil.isNotEmpty(nativeprovince)) {
            		episode.setNativeProvince(nativeprovince);
            	}
            	if(CommonUtil.isNotEmpty(nativecity)) {
            		episode.setNativeCity(nativecity);
            	}
            	if(CommonUtil.isNotEmpty(nativecounty)) {
            		episode.setNativeCounty(nativecounty);
            	}
            	if(CommonUtil.isNotEmpty(nativetown)) {
            		episode.setNativeTown(nativetown);
            	}
            	if(CommonUtil.isNotEmpty(nativeaddress)) {
            		episode.setNativeAddress(nativeaddress);
            	}
            	if(CommonUtil.isNotEmpty(nowprovince)) {
            		episode.setNowProvince(nowprovince);
            	}
            	if(CommonUtil.isNotEmpty(nowcity)) {
            		episode.setNowCity(nowcity);
            	}
            	if(CommonUtil.isNotEmpty(nowcounty)) {
            		episode.setNowCounty(nowcounty);
            	}
            	if(CommonUtil.isNotEmpty(nowtown)) {
            		episode.setNowTown(nowtown);
            	}
            	if(CommonUtil.isNotEmpty(nowaddress)) {
            		episode.setNowAddress(nowaddress);
            	}
            	if(CommonUtil.isNotEmpty(isdeathstr)) {
            		Integer isdeath = Integer.valueOf(isdeathstr);
            		episode.setIsDeath(isdeath);
            	}
            	if(CommonUtil.isNotEmpty(deathdatestr)) {
            		Date deathdate = sdf.parse(deathdatestr);
            		episode.setDeathDate(deathdate);
            	}
            	if(CommonUtil.isNotEmpty(admnumsstr)) {
            		Integer admnums = Integer.valueOf(admnumsstr);
            		episode.setAdmNums(admnums);
            	}
            	if(CommonUtil.isNotEmpty(admdatestr)) {
            		Date admdate = sdf.parse(admdatestr);
            		episode.setAdmDate(admdate);
            	}
            	//获取ID
            	if(CommonUtil.isNotEmpty(admlocid)) {
            		CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
            		cdcLocationEntity.setXcode(admlocid);
            		cdcLocationEntity.setDataId(basedataId);
            		cdcLocationEntity.setIsActive(1);
            		cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
            		
            		//判断科室存在不存在
            		Long locid = null;
            		if(CommonUtil.isEmpty(cdcLocationEntity)) {//不存在  保存到科室表里面
            			cdcLocationEntity = new CdcLocationEntity();
            			cdcLocationEntity.setXcode(admlocid);
            			cdcLocationEntity.setIsActive(1);
            			cdcLocationEntity.setCode("newdep"+admlocid);
            			cdcLocationEntity.setDescription(admlocdesc);
                		cdcLocationEntity.setDataId(basedataId);
                		CdcHospitalEntity cdcHospitalEntity = new CdcHospitalEntity();
            			cdcHospitalEntity.setXcode(admhospid);
            			cdcHospitalEntity.setDataId(basedataId);
            			cdcHospitalEntity.setIsActive(1);
            			cdcHospitalEntity = cdcHospitalMapper.selectOne(cdcHospitalEntity);
            			Long hospid = null;
            			if(CommonUtil.isEmpty(cdcHospitalEntity)) {//不存在  保存到院区表里面
            				cdcHospitalEntity = new CdcHospitalEntity();
            				cdcHospitalEntity.setXcode(admhospid);
                			cdcHospitalEntity.setIsActive(1);
                			cdcHospitalEntity.setDataId(basedataId);
                			cdcHospitalEntity.setCode("newhosp"+admhospid);
                			cdcHospitalEntity.setDescription("newhosp"+admhospid);
                			cdcHospitalMapper.insert(cdcHospitalEntity);
                			hospid = cdcHospitalEntity.getId();
            			}else {
            				hospid = cdcHospitalEntity.getId();
            			}
            			cdcLocationEntity.setHospitalId(hospid);
            			
                		//保存科室
                		cdcLocationMapper.insert(cdcLocationEntity);
                		locid = cdcLocationEntity.getId();
            		}else {
            			locid = cdcLocationEntity.getId();
            		}
            		episode.setAdmLocId(locid);
            	}
            	if(CommonUtil.isNotEmpty(admlocdesc)) {
            		episode.setAdmLocDesc(admlocdesc);
            	}
            	//获取ID
            	if(CommonUtil.isNotEmpty(admwardid)) {
            		CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
            		cdcLocationEntity.setXcode(admwardid);
            		cdcLocationEntity.setDataId(basedataId);
            		cdcLocationEntity.setIsActive(1);
            		cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
            		//判断病区存在不存在
            		Long wardid = null;
            		if(CommonUtil.isEmpty(cdcLocationEntity)) {//不存在  保存到科室表里面
            			cdcLocationEntity = new CdcLocationEntity();
            			cdcLocationEntity.setXcode(admwardid);
            			cdcLocationEntity.setIsActive(1);
            			cdcLocationEntity.setCode("newward"+admwardid);
            			cdcLocationEntity.setDescription(admwarddesc);
                		cdcLocationEntity.setDataId(basedataId);
                		
            			CdcHospitalEntity cdcHospitalEntity = new CdcHospitalEntity();
            			cdcHospitalEntity.setXcode(admhospid);
            			cdcHospitalEntity.setDataId(basedataId);
            			cdcHospitalEntity.setIsActive(1);
            			cdcHospitalEntity = cdcHospitalMapper.selectOne(cdcHospitalEntity);
            			Long hospid = null;
            			if(CommonUtil.isEmpty(cdcHospitalEntity)) {//不存在  保存到院区表里面
            				cdcHospitalEntity = new CdcHospitalEntity();
            				cdcHospitalEntity.setXcode(admhospid);
                			cdcHospitalEntity.setIsActive(1);
                			cdcHospitalEntity.setDataId(basedataId);
                			cdcHospitalEntity.setCode("newhosp"+admhospid);
                			cdcHospitalEntity.setDescription("newhosp"+admhospid);
                			cdcHospitalMapper.insert(cdcHospitalEntity);
                			hospid = cdcHospitalEntity.getId();
            			}else {
            				hospid = cdcHospitalEntity.getId();
            			}
            			cdcLocationEntity.setHospitalId(hospid);
            			
                		//保存科室
                		cdcLocationMapper.insert(cdcLocationEntity);
                		wardid = cdcLocationEntity.getId();
            		}else {
            			wardid = cdcLocationEntity.getId();
            		}
            		episode.setAdmWardId(wardid);
            	}
            	if(CommonUtil.isNotEmpty(admwarddesc)) {
            		episode.setAdmWardDesc(admwarddesc);
            	}
            	if(CommonUtil.isNotEmpty(admroom)) {
            		episode.setAdmRoom(admroom);
            	}
            	if(CommonUtil.isNotEmpty(admbed)) {
            		episode.setAdmBed(admbed);
            	}
            	//获取ID
            	if(CommonUtil.isNotEmpty(admdoctorid)) {
            		CdcUserEntity cdcUserEntity = new CdcUserEntity();
            		cdcUserEntity.setXcode(admdoctorid);
            		cdcUserEntity.setDataId(basedataId);
            		cdcUserEntity.setIsActive(1);
            		cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
            		//判断人员存在不存在
            		Long doctorid = null;
            		if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
            			cdcUserEntity = new CdcUserEntity();
            			cdcUserEntity.setXcode(admdoctorid);
                		cdcUserEntity.setIsActive(1);
            			cdcUserEntity.setCode("newuser"+admdoctorid);
            			cdcUserEntity.setName("newuser"+admdoctorid);
                		cdcUserEntity.setDataId(basedataId);
                		//保存用户
                		cdcUserMapper.insert(cdcUserEntity);
                		doctorid = cdcUserEntity.getId();
            		}else {
            			doctorid = cdcUserEntity.getId();
            		}
            		episode.setAdmDoctorId(doctorid);
            	}
            	if(CommonUtil.isNotEmpty(admdoctorname)) {
            		episode.setAdmDoctorName(admdoctorname);
            	}
            	if(CommonUtil.isNotEmpty(dishdatestr)) {
            		Date dishdate = sdf.parse(dishdatestr);
            		episode.setDishDate(dishdate);
            	}
            	if(CommonUtil.isNotEmpty(admdaysstr)) {
            		Integer admdays = Integer.valueOf(admdaysstr);
            		episode.setAdmDays(admdays);
            	}
            	
            	if(CommonUtil.isNotEmpty(isnewlystr)) {
            		Integer isnewly = Integer.valueOf(isnewlystr);
            		episode.setIsNewly(isnewly);
            	}
            	
            	if(CommonUtil.isNotEmpty(isnewbornstr)) {
            		Integer isnewborn = Integer.valueOf(isnewbornstr);
            		episode.setIsNewborn(isnewborn);
            	}
            	
            	if(CommonUtil.isNotEmpty(bornweightstr)) {
            		Integer bornweight = Integer.valueOf(bornweightstr);
            		episode.setBornWeight(bornweight);
            	}
            	
            	if(CommonUtil.isNotEmpty(admweightstr)) {
            		Integer admweight = Integer.valueOf(admweightstr);
            		episode.setAdmWeight(admweight);
            	}
            	
            	if(CommonUtil.isNotEmpty(iscodedstr)) {
            		Integer iscoded = Integer.valueOf(iscodedstr);
            		episode.setIsCoded(iscoded);
            	}
            	if(CommonUtil.isNotEmpty(codedatestr)) {
            		Date codedate = sdf.parse(codedatestr);
            		episode.setCodeDate(codedate);
            	}
            	episode.setDataId(dataId);
            	episode.setIsActive(1);
            }else {//逻辑删除
            	episode.setIsActive(0);
            }
            //先判断是不是有该对象
            Wrapper<CdcEpisodeEntity> wrapper =  new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episode.getXcode());
            wrapper.eq("data_id", dataId);
            int count = this.selectCount(wrapper);
            if(count>0) {
            	this.update(episode, wrapper);
            }else {
            	this.insert(episode);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        } catch (Exception e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        }
		return "<response><status>1</status><message>ok</message></response>";
	}

    @Override
    public String getNameByEpisodeId(Long episodeId) {
        return this.baseMapper.selectByEpisodeId(episodeId);
    }

}